<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${bussPackage}.dao.${entityPackage}.${className}Dao" > 

<!-- 	${tableName}  ${tableComment}  ${className}  -->

<!-- Result Map-->
<resultMap id="Base_Result_Map" type="${bussPackage}.entity.${entityPackage}.${className}" >
<#list columnList as columnEntity>
	<!-- ${columnEntity.columnComment}-->
	<result property="${columnEntity.javaname}" column="${columnEntity.columnName}" />
</#list>
</resultMap>


<!-- 查询列名 -->
<sql id="Base_Columns" >
	<#list columnList as columnEntity>t.${columnEntity.columnName}<#if columnEntity_has_next> ,</#if></#list>
</sql>
   
<!-- 表名-->
<sql id="Base_Table_Name">
	${tableName} t
</sql>
 
 <!-- 根据id查询 , 返回一个对象 -->
<select id="queryById"  parameterType="Object"  resultMap="Base_Result_Map"  >
	select <include refid="Base_Columns" />
	from   <include refid="Base_Table_Name" />
	where t.id = ${r"#{id}"}
</select>


<!-- 新增 -->
<insert id="add" parameterType="Object" >
	<selectKey resultType="${pk.javaDataType}"  keyProperty="id" order="AFTER" >
		SELECT LAST_INSERT_ID()
	</selectKey>
  
	insert into	${tableName} 
		(<#list columnList as columnEntity>${columnEntity.columnName}<#if columnEntity_has_next> ,</#if></#list>)
	values 
		( <#list columnList as columnEntity>${r"#{"}${columnEntity.javaname}${r"}"}<#if columnEntity_has_next> ,</#if></#list>  )
</insert>



<!-- 更新  -->
<update id="update" parameterType="Object">
	update <include refid="Base_Table_Name" />
	set <#list columnList as columnEntity><#if columnEntity.pki==false  >${columnEntity.columnName}= ${r"#{"}${columnEntity.javaname}${r"}"}<#if columnEntity_has_next> ,</#if></#if></#list>
	where id=${r"#{id}"}
</update>

<!-- 删除 -->
<delete id="delete" parameterType="Object">
	delete from ${tableName} where id = ${r"#{id}"}
</delete>


<!-- 普通条件查询 , 返回List(Map) -->
<select id="queryMapList" parameterType="Object" resultMap="Base_Result_Map" >
	select <include refid="Base_Columns" />
	from  <include refid="Base_Table_Name" />
	<include refid="Base_Where_Clause" />
</select>

<!-- 条件查询 , 返回数量-->
<select id="queryCountByCondition"  parameterType="Object"  resultType="java.math.BigInteger" >
	select count(1) from <include refid="Base_Table_Name" />
	<include refid="Base_Where_Clause"/>
</select>


<!-- 分页条件查询 , 返回List(${className}) -->
<select id="queryPagingByCondition" parameterType="Object" resultMap="Base_Result_Map" >
	select <include refid="Base_Columns" />
	from  <include refid="Base_Table_Name" />
	<include refid="Base_Where_Clause" />
	<if test="pager.orderCondition != null and pager.orderCondition !=''  " >
		${r"${pager.orderCondition}"}
	</if>
	<if test="pager.mysqlQueryCondition != null and pager.mysqlQueryCondition != '' " >
		${r"${pager.mysqlQueryCondition}"}
	</if>
</select>


<!-- where 查询条件 -->
<sql id="Base_Where_Clause">
	where 1=1
	<trim  suffixOverrides="," >
<#list columnList as columnEntity>
<#if columnEntity.javaDataType == 'String' >
		<if test="${columnEntity.javaname} !=null and ${columnEntity.javaname} !=''  " >
			and t.${columnEntity.columnName} like concat('%', ${r"#{"}${columnEntity.javaname}${r"}"}, '%')
		</if>
<#elseif columnEntity.javaDataType == 'java.util.Date'  >
		<if test="${columnEntity.javaname} !=null and ${columnEntity.javaname} !=''  " >
			and t.${columnEntity.columnName} = ${r"#{"}${columnEntity.javaname}${r"}"}
		</if>
		<if test="${columnEntity.javaname}_1 !=null and ${columnEntity.javaname}_1 !=''  " >
			and t.${columnEntity.columnName} <![CDATA[ >=]]> ${r"#{"}${columnEntity.javaname}_1${r"}"}
		</if>
		<if test="${columnEntity.javaname}_2 !=null and ${columnEntity.javaname}_2 !=''  " >
			and t.${columnEntity.columnName} <![CDATA[ <=]]> concat(${r"#{"}${columnEntity.javaname}_2${r"}"}, ' 23:59:59')
		</if>
<#else>
		<if test="${columnEntity.javaname} !=null  " >
			and t.${columnEntity.columnName} = ${r"#{"}${columnEntity.javaname}${r"}"}
		</if>
</#if>

		</#list>
	</trim>
	order by ${pk.columnName} desc
</sql>



</mapper>   
